<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="/css/nav.css" />
		<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
		<title></title>
		<style type="text/css">
			.message_zl{
			margin-left:53px;
			padding-left:27px;
			font-size:18px;
			color: #333;
			margin-bottom:30px;
			background: url(../img/nav/dd.png) no-repeat 2px;
		
		}
		.area select {
    height: 36px;
    width: 81.5px;
}
		</style>
	</head>
	<body>
		<div class="content_zl">
			<p class="title_zl">基本资料</p>
			<div class="tishi">
				请完善以下信息，方便我们更好的为您服务
			</div>
			<p class="message_zl">基本信息</p>
			<div class="link">
				<span style="color: red; margin-right:20px;opacity: 0;">*</span><span style="margin-right:50px;">联系人</span>
				<input type="" name="" id="" value="" class="link_man" placeholder="请输入姓名" />
				<span style="color: red; margin-left:20px;">*</span>
			</div>
			<div class="link">
				<span style="color: red; margin-right:20px;margin-top:12px;opacity: 0;">*</span><span style="margin-right:34px;">联系方式</span>
				<input type="" name="" id="" value="" class="link_num" placeholder="请输入手机号码" readonly="readonly" />
				<span style="color: red; margin-left:20px;">*</span>
			</div>
			<div class="detail_zl" style="margin-top:63px;">
				<p class="message_zl">详细信息<!-- <span style="color:#00478b;">(认证中)</span>--></p>
			</div>
			<div class="link" style="margin-left:80px;">
				<span style="width:98px;">单位名称</span>
				<input type="" name="" value="" class="danwei" placeholder="请输入单位名称" />
			</div>
			<div class="link" style="margin-left:80px;margin-top:14px;">
				<span style="width:98px;">职位</span>
				<input type="" name="" value="" class="zhiwei" placeholder="职位"/>
			</div>
			<div class="link area" style="margin-left:80px;margin-top:14px;">
				<span style="width:98px;">单位地址</span>
				<select class="L0" id='L0'>

				</select>
				<select class="L1" id="L1">

				</select>
				<select class="L2" id="L2">

				</select>
			</div>
			<div style="color:red;display: none;margin-left:80px;margin-top:12px;" class="message">

			</div>
			<div class="btn" style="margin-left:184px;">
				<button class="submit">保存</button>
			</div>
		</div>
	</body>
</html>
<script type="text/javascript">
	$(document).ready(function() {
		var arr0 = []
		var arr1 = []
		var arr2 = []
		var arr11 = []
		var arr22 = []
		//调用获取下拉接口
		getArea()
		// setTimeout(function() {
			
		// }, 2000)

		//		获取地址下拉接口
		function getMessage() {
			var phone = "<? echo $phone;  ?>";
			$('.link_num').val(phone)
			var param = { "personal_mobile": phone,"fromRequest":'tanwin'}
			$.ajax({
				url: "http://api.crm.tanwin.cn/ctm/v1/getPersonalinfo",
				type: "Post",
				dataType: "json",
				data: param,
				success: function(response) {
					console.log(response)
					if(response.code == 100) {

						$('.link_num').val(response.data.personal_mobile)
						$('.link_man').val(response.data.personal_name)
						$('.danwei').val(response.data.company_name)
						$('.zhiwei').val(response.data.position)
						var address = response.data.company_address?response.data.company_address.split(','):[2,52,500]
						
//						var address = [3,37,412]
						for(var i in arr0) {
						$(".L0").append("<option value='" + arr0[i].district_id + "'>" + arr0[i].district + "</option>");
						}
						for(var i in arr1) {
							if(arr1[i].pid == 2) {
								arr11.push(arr1[i])
							}
						}
						$("#L0 option[value='" + address[0] + "']").attr('selected', 'selected')
						arr11 = []
						$("#L1").empty();
						for(var i in arr1) {
							if(arr1[i].pid == address[0]) {
								arr11.push(arr1[i])
							}
						}
						for(var i in arr11) {
							$(".L1").append("<option value='" + arr11[i].district_id + "'>" + arr11[i].district + "</option>");
						}
						$("#L1 option[value='" + address[1] + "']").attr('selected', 'selected')
//						第三层
						arr22 = []
						$("#L2").empty();
						for(var i in arr2) {
//							console.log(address[2])
							if(arr2[i].pid == address[1]) {
								arr22.push(arr2[i])
							}
						}
						for(var i in arr22) {
							$(".L2").append("<option value='" + arr22[i].district_id + "'>" + arr22[i].district + "</option>");
						}
						$("#L2 option[value='" + address[2] + "']").attr('selected', 'selected')
					} else {
						$('.message').css('display', 'block').html(response.msg)
					}

				},
				error: function(data) {
					console.log(data)
				}
			});
		}
		function getArea() {

			var phone = "<? echo $phone;  ?>";
			$('.link_num').val(phone)
			var param = { "personal_mobile": phone,"fromRequest":'tanwin' }
			$.ajax({
				// 
				url: "http://api.crm.tanwin.cn/diction/region",
				type: "Post",
				dataType: "json",
				data: param,
				success: function(response) {
					getMessage()
					console.log(response)
					// console.log(response.data.L0)
					arr0 = response.L0
					arr1 = response.L1
					arr2 = response.L2
//					for(var i in arr0) {
//						$(".L0").append("<option value='" + arr0[i].district_id + "'>" + arr0[i].district + "</option>");
//					}
//					for(var i in arr1) {
//						if(arr1[i].pid == 2) {
//							arr11.push(arr1[i])
//						}
//					}
//					for(var i in arr11) {
//						$(".L1").append("<option value='" + arr11[i].district_id + "'>" + arr11[i].district + "</option>");
//					}
//					for(var i in arr2) {
//						if(arr2[i].pid == 52) {
//							arr22.push(arr2[i])
//						}
//					}
//					for(var i in arr22){
//						$(".L2").append("<option value='" + arr22[i].district_id + "'>" + arr22[i].district + "</option>");
//					}
				},
				error: function(data) {
					console.log()
				}
			});
		}
		$('.L0').change(function() {
			$("#L1").empty();
			arr11 = []
			arr22 = []
			$("#L1").find("option").remove();
			$("#L2").empty();
			var a = $(this).val()
			for(var i in arr1) {
				if(arr1[i].pid == a) {
					arr11.push(arr1[i])
				}
			}
			for(var i in arr11) {
				$(".L1").append("<option value='" + arr11[i].district_id + "'>" + arr11[i].district + "</option>");
			}
			arr11[0]
			change3(arr11[0].district_id)
		})
		//		对第三级区域进行判断
		function change3(a){
			$("#L2").empty();
			arr22 = []
			console.log(a)
//			var a = $(this).val()
			for(var i in arr2) {
				if(arr2[i].pid == a) {
					arr22.push(arr2[i])
				}
			}
			for(var i in arr22) {
				$(".L2").append("<option value='" + arr22[i].district_id + "'>" + arr22[i].district + "</option>");
			}
//			alert(1)
		}
		$('.L1').change(function() {
			$("#L2").empty();
			arr22 = []
			var a = $(this).val()
			for(var i in arr2) {
				if(arr2[i].pid == a) {
					arr22.push(arr2[i])
				}
			}
			for(var i in arr22) {
				$(".L2").append("<option value='" + arr22[i].district_id + "'>" + arr22[i].district + "</option>");
			}
		})
//		$('.L1').click(function() {
//			$("#L2").empty();
//			arr22 = []
//			var a = $(this).val()
//			for(var i in arr2) {
//				if(arr2[i].pid == a) {
//					arr22.push(arr2[i])
//				}
//			}
//			for(var i in arr22) {
//				$(".L2").append("<option value='" + arr22[i].district_id + "'>" + arr22[i].district + "</option>");
//			}
//		})
		var that = this
		$('.submit').on('click', function() {
			var phone = $('.link_num').val()
			var danwei = $('.danwei').val()
			var zhiwei = $('.zhiwei').val()
			//		var linkMan = $('.linkMan').val()
			var linkm = $('.link_man').val()
			if(linkm == '') {
				$('.message').css('display', 'block').html("请输入联系人姓名")
			} else if(phone == '') {
				$('.message').css('display', 'block').html("请输入手机号码")

			} else if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
				$('.message').css('display', 'block').html("请输入正确的手机号")
			} else {
				$('.message').css('display', 'none')
				var one = $('#L0').val()
				var two = $('#L1').val()
				var three = $('#L2').val()
				var phone = "<? echo $phone;  ?>";
				var param = { "personal_mobile": phone, "personal_name": linkm, "company_name": danwei, "position": zhiwei, "company_address": one+','+two+','+three	 ,"fromRequest":'tanwin'}
				console.log(param)
				$.ajax({
					url: "http://api.crm.tanwin.cn/ctm/v1/perfectPersonalInfos",
					type: "Post",
					dataType: "json",
					data: param,
					success: function(response) {
						console.log(response)
						if(response.code == 100) {
							$('.message').css('display', 'block').html('保存成功！')
						} else {
							$('.message').css('display', 'block').html(response.msg)
						}
					},
					error: function(data) {
						console.log(data)
					}
				});
			}
		})
	})
</script>
